package com.tedu.ivos.application.service.impl;

import com.tedu.ivos.application.mapper.ApplicationMapper;
import com.tedu.ivos.application.pojo.dto.ApplicationQuery;
import com.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import com.tedu.ivos.application.pojo.entity.Application;
import com.tedu.ivos.application.pojo.vo.ApplicationVO;
import com.tedu.ivos.application.service.ApplicationService;
import com.tedu.ivos.audit.mapper.AuditMapper;
import com.tedu.ivos.audit.pojo.vo.AuditVO;
import com.tedu.ivos.audit.service.AuditService;
import com.tedu.ivos.base.enums.ApplicationStatusEnum;
import com.tedu.ivos.user.mapper.UserMapper;
import com.tedu.ivos.user.pojo.vo.UserVO;
import com.tedu.ivos.vehicle.mapper.VehicleMapper;
import com.tedu.ivos.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Transactional
@Slf4j
@Service
public class ApplicationServiceImpl implements ApplicationService {

    @Autowired
    private ApplicationMapper applicationMapper;
    @Autowired
    private AuditService auditService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AuditMapper auditMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public void saveApplication(ApplicationSaveParam applicationSaveParam) {
        log.debug("保存申请单业务参数，参数：{}",applicationSaveParam);
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam,application);
        application.setCreateTime(new Date());
        application.setStatus(ApplicationStatusEnum.PENGDING.getCode());
        applicationMapper.insert(application);
        auditService.insertAudit(application);
    }

    @Override
    public List<ApplicationVO> selectApplicationList(ApplicationQuery applicationQuery) {
        log.debug("查询申请单业务参数,{}", applicationQuery);
        List<ApplicationVO> list = applicationMapper.selectApplicationList(applicationQuery);
        // 遍历list,为每个VO对象补全审批人数据
        for (int i = 0; i < list.size(); i++){
            ApplicationVO applicationVO = list.get(i);
            // 自定义一个方法用于给申请单VO补全数据
            assignAuditUserList(applicationVO);
        }

        return list;
    }

    @Override
    public void cancelApplication(Long id) {
        log.debug("撤销申请单业务,{}", id);
        Application application = new Application();
        application.setId(id);
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
        auditMapper.deleteByApplicationId(id);
    }

    @Override
    public void distribute(Long applicationId, Long vehicleId) {
        log.debug("为申请单分配车辆业务,{},{}", applicationId, vehicleId);
        Application application = new Application();
        application.setId(applicationId);
        application.setVehicleId(vehicleId);
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);

        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("2");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateVehicle(vehicle);
    }

    @Override
    public void back(Long applicationId, Long vehicleId) {
        log.debug("还车业务,{},{}", applicationId, vehicleId);
        Application application = new Application();
        application.setId(applicationId);
        application.setStatus(ApplicationStatusEnum.END.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.back(application);

        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("1");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateVehicle(vehicle);
    }


    private void assignAuditUserList(ApplicationVO applicationVO){
        List<Long> auditUserIdList = new ArrayList<>();
        List<String> auditUsernameList = new ArrayList<>();
        List<AuditVO> auditVOList = auditService.selectAuditByApplicationId(applicationVO.getId());

        for (int i = 0; i < auditVOList.size(); i++){
            AuditVO auditVO = auditVOList.get(i);
            Long id = auditVO.getAuditUserId();
            auditUserIdList.add(id);
            UserVO userVO = userMapper.selectById(id);
            auditUsernameList.add(userVO.getUsername());
        }
        StringJoiner stringJoiner = new StringJoiner(",");
        for (String username : auditUsernameList){
            stringJoiner.add(username);
        }
        applicationVO.setAuditUserIdList(auditUserIdList);
        applicationVO.setAuditUsernameList(stringJoiner.toString());
    }
}
